Skip to content

Rotating an Object

In build mode rotation can be accomplished as an additional parameter while constructing your primitive. Each 2D or 3D
object has a rotation parameter.

Rotating an object by build parameter

First we create a rectangle:

from build123d import *
from ocp_vscode import *
set_defaults(reset_camera=Camera.KEEP, black_edges=True,center_grid=True)

with BuildSketch() as TestRotationSketch:
    # Creates a rectangle
    Rectangle(6,5)

show_all()

We then rotatet that rectangle by 45 degrees.

from build123d import *
from ocp_vscode import *
set_defaults(reset_camera=Camera.KEEP, black_edges=True,center_grid=True)

with BuildSketch() as TestRotationSketch:
    # Creates a rectangle and rotates it by 45 degrees
    # EACH 2D or 3D object as a rotation parameter
    Rectangle(6,5,rotation=45)

show_all()

This can also be written as:

from build123d import *
from ocp_vscode import *
set_defaults(reset_camera=Camera.KEEP, black_edges=True,center_grid=True)

with BuildSketch() as TestRotationSketch:
    # Creates a rectangle and rotates it by 45 degrees
    Rectangle(6,5,45)

show_all()

Rotation by Location Object

Creating a location object.

from build123d import *
from ocp_vscode import *
set_defaults(reset_camera=Camera.KEEP, black_edges=True,center_grid=True)

with BuildSketch() as TestRotationSketch:
    # Creates a Locations LIST which contains a Location object
    with Locations(Rotation(0,0,45)):
        # Creates a rectangle and rotates it by 45 degrees
        Rectangle(6,5)

show_all()

This Version ist different in that it creates a location OBJECT in a location LIST to show this better we switch to a cube/box.

Danger

Location creates an object!

Locations expects a LIST of location objects (Locations ! <== locationS)

from build123d import *
from ocp_vscode import *
set_defaults(reset_camera=Camera.KEEP, black_edges=True,center_grid=True)

with BuildPart() as TestRotationSPart:
    # Creates a Locations LIST which contains a Location object
    with Locations(Rotation(0,0,45)):
        # Creates a cube and rotates it by 45 degrees
        Box(6,5,3)

show_all()

from build123d import *
from ocp_vscode import *
set_defaults(reset_camera=Camera.KEEP, black_edges=True,center_grid=True)

with BuildPart() as TestRotationSPart:
    # Creates a Locations LIST which contains a Location object
    with Locations(Rotation(0,30,45)):
        # Creates a cube and rotates it by Y=30 and Z=45 degrees
        Box(6,5,3)

show_all()

Now the rotation took place in two dimensions Y and Z. The distincion still is that this time a we created a location. A Locations LIST can have several locations.
Also each Location can have a position AND rotation!

Rotation AND placing by Location Object

from build123d import *
from ocp_vscode import *
set_defaults(reset_camera=Camera.KEEP, black_edges=True,center_grid=True)

with BuildPart() as TestRotationSPart:
    # Creates TWO locations in the Locations LIST
    with Locations(Location((0,0,0),(0,30,45)),Location((10,10,0),(45,20,0))):
        # Creates a cube at EACH Location in the Location LIST and rotates it
        Box(6,5,3)

show_all()

Now we created two location objects in the Locations LIST. Location can take several parameter COMBINATIONS:

1) Just a Location (point in space): Location((x,y,z))
2) Location and Rotation: Location((x,y,z),(x_angle,y_angle,z_angle))
3) Location on a plane: Location(Plane.YZ)
4) Location on a Plane with a point on that plane: Location(Plane.XZ,(x,y,z))

In VScode you can see that Location takes up to 10 different combinations of input from planes, rotations, vectors, angles and orderings.

The Location object is the object to place your objects and it can be written much shorter and also nested.

Version 1 - two locations short form

from build123d import *
from ocp_vscode import *
set_defaults(reset_camera=Camera.KEEP, black_edges=True,center_grid=True)

with BuildPart() as TestRotationSPart:

    with Locations((0,0,0),(10,10,0)):
        # Creates two cubes at two locations
        Box(6,5,3)

show_all()

Version 2- two locations prepared for nesting

from build123d import *
from ocp_vscode import *
set_defaults(reset_camera=Camera.KEEP, black_edges=True,center_grid=True)

with BuildPart() as TestRotationSPart:

    with Locations((0,0,0)):
        # Creates a cube at 0,0,0
        Box(6,5,3)
    with Locations((10,10,0)):
        # Creates a cube at 10,10,0
        Box(6,5,3)

show_all()

Version 3 - two locations with a nested and parameter rotation

from build123d import *
from ocp_vscode import *
set_defaults(reset_camera=Camera.KEEP, black_edges=True,center_grid=True)

with BuildPart() as TestRotationSPart:
    # Places an object at 0,0,0
    with Locations((0,0,0)):
        # Creates a cube and rotates it by 45 degrees
        Box(6,5,3,rotation=(0,30,45))
    # Places an object at 10,10,0   
    with Locations((10,10,0)):
        # Rotates an object
        with Locations(Rotation(45,20,0)):
            # Creates a cube
            Box(6,5,3)

show_all()

As you can see this is exactly the same as one of our former examples.

Version 4 - two locations with a nested and parameter rotation and placing on a different plane

from build123d import *
from ocp_vscode import *
set_defaults(reset_camera=Camera.KEEP, black_edges=True,center_grid=True)

with BuildPart() as TestRotationSPart:

    with Locations((0,0,0)):
        # Creates a cube and rotates it by 45 degrees
        Box(6,5,3,rotation=(0,30,45))
    with Locations((10,10,0)):
        with Locations(Rotation(45,20,0)):
            # Creates a cube and rotates it by 45 degrees
            Box(6,5,3)

show_all()